package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.goods

import android.os.Bundle
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.adapter.multi.JssNewMultipleAdapter
import com.example.library_common.album.utils.Utils
import com.example.library_common.album.view.grid_view.XGridLayout
import com.example.library_common.album.view.grid_view.adapter.CommAdapter
import com.example.library_common.album.view.grid_view.adapter.XQuickViewHolder
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.global.AppGlobals
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.SecKillSubBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.SubGoodsCommentBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.bean.NewSaleFunEntity
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

/**
 * 商品评价
 * */
class SubGoodsCommentFragment(override val comment: SubGoodsCommentData) :
    SimpleListFragment<SubGoodsCommentBean>(), SubGoodsComment {

    companion object {
        fun newInstance(comment: SubGoodsCommentData): SubGoodsCommentFragment {
//            val titleStr = AppGlobals.getApplication().resources.getString(title)
            val args = Bundle()
            val fragment = SubGoodsCommentFragment(comment)
            fragment.arguments = args
            return fragment
        }
    }

    private var goodsId: String? = ""

    override fun setGoodsId(goodsId: String?) {
        this.goodsId = goodsId
    }
//    private val imgs = listOf(
//        "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601014062971&di=af21ace7e3ecdd8dd39c37c622223f3f&imgtype=0&src=http%3A%2F%2Ft8.baidu.com%2Fit%2Fu%3D3571592872%2C3353494284%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D1200%26h%3D1290",
//        "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601014062969&di=00522c6e88bff73e035000a23e26c070&imgtype=0&src=http%3A%2F%2Ft8.baidu.com%2Fit%2Fu%3D2247852322%2C986532796%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D1280%26h%3D853",
//        "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601014137987&di=8c4eb43cfc715e26ee8af5a1309a3dd5&imgtype=0&src=http%3A%2F%2Ft7.baidu.com%2Fit%2Fu%3D1179872664%2C290201490%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D1280%26h%3D854",
//        "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601014137986&di=e60a3cfd8f68ff291e2259112412fd3c&imgtype=0&src=http%3A%2F%2Ft9.baidu.com%2Fit%2Fu%3D2266751744%2C4253267866%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D1280%26h%3D854",
//        "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601014137986&di=a62381ffc548330267c49184f3b4327b&imgtype=0&src=http%3A%2F%2Ft9.baidu.com%2Fit%2Fu%3D3949188917%2C63856583%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D1280%26h%3D875",
//        "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601014137985&di=e56615cccacb8d6fc38d7c8410db614e&imgtype=0&src=http%3A%2F%2Ft8.baidu.com%2Fit%2Fu%3D4100756023%2C1345858297%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D1280%26h%3D854"
//    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isEnableLoadMore = false
        isAddDividerItemDecoration = false
        mSuperBackPressedSupport = false
    }


    override fun getItemLayout() = R.layout.sub_goods_comment_fragment

    override fun convertItem(helper: JssBaseViewHolder?, item: SubGoodsCommentBean?) {
        helper?.setImageNetUrl(R.id.mCircleImageView, item?.goodsLogo)
            ?.setText(R.id.goodsName, item?.goodsName)
            ?.setText(R.id.typeTv, "颜色：${item?.goodsColor}  尺寸：${item?.goodsSize}")
            ?.setRating(R.id.goods_ratingBar, item?.speedStar ?: 0)


//        helper?.setImageNetUrl(R.id.mCircleImageView,"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601015219975&di=5477ebf12598291b79fd534c9d7b892e&imgtype=0&src=http%3A%2F%2Ft9.baidu.com%2Fit%2Fu%3D1307125826%2C3433407105%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D5760%26h%3D3240")
//        val mXGridLayout: XGridLayout? = helper?.findView22(R.id.mXGridLayout)
//        val mAdapter = object
//            : CommAdapter<String>(_mActivity, R.layout.sub_goods_grid_comment_fragment) {
//            override fun convert(holder: XQuickViewHolder?, item: String?, position: Int) {
//                val maxItem = mXGridLayout?.maxItem ?: 1
//                val flag = maxItem < imgs.size && position == maxItem - 1
//                holder?.displayImage(R.id.mImageView, item)
//                    ?.setText(R.id.leftAvatar, "+${imgs.size - (mXGridLayout?.maxItem ?: 1)}")
//                    ?.setVisible(R.id.leftAvatar, flag)
//            }
//
//        }
////        mAdapter.addAll(imgs)
//        mXGridLayout?.setAdapter(mAdapter, lifecycle)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(SubGoodsCommentBean::class.java).endSubType().build()
    }

    override fun netRequest() {
//        val jsonFromRaw = Utils.getJsonFromRaw(_mActivity, R.raw.temp_fack_data)
//        parseDate(jsonFromRaw)
//        val jsonFromRaw = ""
//        parseDate(jsonFromRaw)

        val map = mapOf(
            "assessType" to comment.assessType,
            "goodsId" to goodsId,
            "pageCount" to Int.MAX_VALUE,
            "pageIndex" to pageIndex
        )

        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.queryGoodsAssessList)
            .success {
                parseDate(it)
            }.failure { onFailed() }
            .error { _, _ -> onFailed() }
            .build().post()

    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)
        val assessList=obj["assessList"]?.toString()?:""
        val array = JSON.parseArray(assessList, SubGoodsCommentBean::class.java)
        loadListDate(array)
    }


}